package com.zby.demo.controller.back;

import com.zby.demo.dao.ClassDao;
import com.zby.demo.dao.CourseDao;
import com.zby.demo.dao.StudentDao;
import com.zby.demo.entity.Class;
import com.zby.demo.entity.Course;
import com.zby.demo.entity.SelectCourseStudents;
import com.zby.demo.entity.Student;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreateDate 2020/12/18 18:07
 */

@Controller
public class CourseController {
    @Autowired
    CourseDao courseDao;
    @Autowired
    ClassDao classDao;
    @Autowired
    StudentDao studentDao;

    /*
     * @Param: [model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/18 18:20
     * @Description: 查询所有课程并请求转发到课程列表页面
     */
    @RequestMapping("/back/course/list")
    public String toCourseList(Model model) {
        List<Course> courses = courseDao.queryAllCourse();
        model.addAttribute("courses", courses);

        return "back/course/list";
    }

    /*
     * @Param: [course]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/19 23:11
     * @Description: 课程添加，后重定向到课程列表
     */
    @PostMapping("/back/course/add")
    public String addCourse(Course course) {
        courseDao.addCourse(course);
        if (course.getCourseType().equals("7")) {
            List<Student> students = studentDao.queryAllStudent();
            for (Student student : students) {
                studentDao.setSelectCourse(student.getStudentId(), course.getCourseId());
            }
        }
        return "redirect:/back/course/list";
    }

    /*
     * @Param: [courseId]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/19 23:11
     * @Description: 课程删除功能，后重定向到课程列表。注意点：删除课程之前需要删除对应的选课信息。
     */
    @GetMapping("/back/course/delete/{courseId}")
    public String deleteStudent(@PathVariable("courseId") String courseId) {
        courseDao.deleteSelectCourse(courseId);
        courseDao.deleteCourse(courseId);
        return "redirect:/back/course/list";
    }

    /*
     * @Param: [deleteall]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:30
     * @Description: 批量删除课程。注意点同上
     */
    @PostMapping("/back/course/delete")
    public String deleteCourses(@Param("deleteall") String deleteall) {
        String[] deletecourses = deleteall.split(",");
        for (String courseId : deletecourses) {
            courseDao.deleteSelectCourse(courseId);
            courseDao.deleteCourse(courseId);
        }
        return "redirect:/back/course/list";
    }

    /*
     * @Param: [course]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/19 23:12
     * @Description: 修改课程信息，后重定向到课程列表
     */
    @PostMapping("/back/course/info")
    public String updateCourse(Course course) {
        courseDao.updateCourse(course);
        return "redirect:/back/course/list";
    }

    /*
     * @Param: [model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/19 23:12
     * @Description: 查询课程选报情况，并请求转发到课程选报详情页面
     */
    @GetMapping("/back/course/selectcourse")
    public String toCourseSelectCourse(Model model) {
        List<SelectCourseStudents> selectCourseStudents = newList(courseDao.selectCourseStudents());
        model.addAttribute("selectCourseStudents", selectCourseStudents);
        List<Class> classes = classDao.queryAllClass();
        model.addAttribute("classes", classes);
        return "back/course/selectcourse";
    }

    //更新课程状态
    List<SelectCourseStudents> newList(List<SelectCourseStudents> selectCourseStudents) {
        List<SelectCourseStudents> list = new ArrayList<>();
        long time = new Date().getTime();
        for (SelectCourseStudents scs : selectCourseStudents) {
            Course course = scs.getCourse();
            if (time - course.getBeginDate().getTime() >= 0) {
                if (course.getEndDate().getTime() + 1000 * 60 * 60 * 24 > time) {
                    course.setStatus("进行中");
                    list.add(0, scs);
                } else {
                    course.setStatus("已结束");
                    list.add(scs);
                }
            } else {
                course.setStatus("未开始");
                list.add(scs);
            }
        }
        return list;
    }
}
